<?php
/*
 * СПВ
 **/
class SP_Loader
{	
	static private $classes=array();
	static private $models=array();
	static private $lib_dirs=array();//'libs';
	static private $model_dirs=array();//'models';
	static private $include_path;
	
	static public function getSpPath()
	{
		return dirname(__FILE__).'/';
	}
	
	static public function setIncludePath($value)
	{
		self::$include_path=$value;
	}
	
	static public function getIncludePath()
	{
		return self::$include_path.'/';
	}
	
	static public function load($name)
	{
		require_once(self::getIncludePath().$name);
	}
	
	static public function loadPath($path)
	{
		require_once($path);
		return true;
	}
	
	static public function addModelDir($dirs, $pre_path='')
	{
		$dirs=(array)$dirs;
		foreach($dirs as $dir)
		{
			self::$model_dirs[]=$pre_path.$dir;
		}
	}
	
	static public function deleteModelDir($dir)
	{
		$key=array_search($dir, self::$model_dirs);
		if( $key!==false )
		{
			unset(self::$model_dirs[$key]);
		}
	}
	
	static public function clearModelDirs($dir)
	{
		self::$model_dirs=array();
	}
	
	//::addModel(array('HtmlTable','HtmlTableField'),'html_objects.php')
	static public function addModel($names, $dir)
	{
		$names=(array)$names;
		foreach($names as $name)
		{
			self::$models[strtolower($name)]=$dir;
		}
	}
	
	static public function hasModel($name)
	{
		return array_key_exists(strtolower($name), self::$models);
	}
	
	static public function getModelDir($name)
	{
		return self::$models[strtolower($name)];
	}
	
	static public function loadModel($name)
	{
		if( self::hasModel($name) )
		{
			foreach(self::$model_dirs as $dir)
			{
				$path=self::helper_get_path($dir).self::getModelDir($name);
				if( self::helper_file_exists($path) )
				{
					require_once($path);
					return true;
				}
			}
			return false;
		}
		
		$names['name']=implode('/',explode('_', $name));
		$names['lower_name']=strtolower($names['name']);
		$names['camel']=implode('_', self::helper_split_camel_case($name, false));
		$names['lower_camel']=strtolower($names['camel']);
		foreach(self::$model_dirs as $dir)
		{
			foreach($names as $new_name)
			{
				$path=self::helper_get_path($dir).$new_name.'.php';
				if( self::helper_file_exists($path) )
				{
					return self::loadPath($path);
				}
			}
		}
		return false;
	}
	
	static public function addLibDir($dirs)
	{
		$dirs=(array)$dirs;
		foreach($dirs as $dir)
		{
			self::$lib_dirs[]=$dir;
		}
	}
	
	static public function deleteLibDir($dir)
	{
		$key=array_search($dir, self::$lib_dirs);
		if( $key!==false )
		{
			unset(self::$lib_dirs[$key]);
		}
	}
	
	static public function clearLibDirs($dir)
	{
		self::$lib_dirs=array();
	}
	
	//::addLib(array('HtmlTable','HtmlTableField'),'htmllib.php')
	static public function addClass($names, $dir)
	{
		$names=(array)$names;
		foreach($names as $name)
		{
			self::$classes[strtolower($name)]=$dir;
		}
	}
	
	static public function hasClass($name)
	{
		return array_key_exists(strtolower($name), self::$classes);
	}
	
	static public function getClassDir($name)
	{
		return self::$classes[strtolower($name)];
	}
	
	static public function loadClass($name)
	{
		if( self::hasClass($name) )
		{
			foreach(self::$lib_dirs as $dir)
			{
				$path=self::helper_get_path($dir).self::getClassDir($name);
				if( self::helper_file_exists($path) )
				{
					require_once($path);
					return true;
				}
			}
			return false;
		}
		
		$names['name']=implode('/',explode('_', $name));
		$names['lower_name']=strtolower($names['name']);
		$names['camel']=implode('/', self::helper_split_camel_case($name, false));
		$names['lower_camel']=strtolower($names['camel']);
		foreach(self::$lib_dirs as $dir)
		{
			foreach($names as $new_name)
			{
				$path=self::helper_get_path($dir).$new_name.'.php';
				if( self::helper_file_exists($path) )
				{
					return self::loadPath($path);
				}
			}
		}
		return false;
	}
	
	static private function helper_file_exists($path)
	{
		return (file_exists($path) && is_file($path));
	}
	
	static private function helper_get_path($dir)
	{
		return self::getIncludePath().$dir.'/';
	}
	
	static private function helper_split_camel_case($string, $lower = true)
	{
	  $array = preg_split('/([A-Z][^A-Z]*)/', $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
	  if ($lower) 
	  {
		$array = array_map(strtolower, $array);
	  }
	  return $array;
	}
	
	static public function enableAutoload()
	{
		spl_autoload_register(array(self, 'autoload'));
	}
	
	static private function autoload($name)
	{
		if ( self::loadClass($name) ) return true;
		if ( self::loadModel($name) ) return true;
		return false;
	}
}